package com.example.hehecat.yuejian1.mvp.presenter;

import android.support.annotation.NonNull;
import android.util.Log;


import com.example.hehecat.yuejian1.mvp.contract.LoginContract;
import com.example.hehecat.yuejian1.mvp.model.LoginModel;
import com.example.hehecat.yuejian1.mvp.model.bean.UserBean;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hehecat on 2017/11/30.
 */

public class LoginPresenter implements LoginContract.Presenter {
    private LoginContract.Model model;
    private LoginContract.View view;

    public LoginPresenter(LoginContract.View view) {
        this.view = view;
    }


    @Override
    public void login(String usernaeme, String password) {
        model = new LoginModel();
        model.login(usernaeme, password, new Callback<UserBean>() {
            @Override
            public void onResponse(@NonNull Call<UserBean> call, @NonNull Response<UserBean> response) {

                Log.d("userid",String.valueOf(response.body().getUserid()));
                if (!response.body().getUserid().equals("-1")){
                    view.showdata("登陆成功");
                    MyStaticVariable variable =  MyStaticVariable.newInstance();
                    variable.setUserid(Integer.valueOf(response.body().getUserid()));
                    view.moveToMainView();
                }else {
                    view.showdata("登陆失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserBean> call, @NonNull Throwable t) {
                Log.d("login",t.toString());
            }
        });

    }
}
